<template>
	<view>
		<pageheader :setting="settingdata"></pageheader>
		<view class="manageimgbox">
			<view class="managebox">
				<image :src="suborderimags" mode="widthFix"></image>
				<text @click="torichtext">操作指南</text>
			</view>
		</view>
		<view class="contentsbox">
			<navigator :url="'/home/station/stationpage?id='+goods.merchant_info.client_uid" hover-class="none"
				class="contents_shops">
				<view class="storesinfo">
					<text class="name">卖家：{{goods.merchant_info.nickname}}</text>
					<image :src="goods.merchant_info.mimage" v-if="goods.merchant_info.mimage" mode="heightFix"></image>
					<view class="xinyong_box">
						<view class="xin">信</view>
						<view class="xin_number">{{goods.merchant_info.credit_value}}</view>
						<view class="shiming" v-if="goods.merchant_info.realname">实</view>
						<view class="qiye" v-if="goods.merchant_info.enterprise">企</view>
					</view>
				</view>
				<u-icon name="arrow-right" color="#999999" size="28"></u-icon>
			</navigator>
			<view class="shops_details">
				<image :src="goods.image" mode="aspectFill"></image>
				<view class="details_info">
					<text class="detailname clamp1">{{goods.title}}</text>
					<view class="">
						{{goods.spec_value}}
					</view>
					<view class="hot_info">
						<view class="hot_price"><text>￥</text>{{goods.money}}<text>/{{goods.unit}}</text></view>
						<text class="hot_looks">{{goods.view}}人查看</text>
					</view>
					<view class="shopsadd">
						<text>{{goods.address}}</text>
						<text class="distance">距离{{goods.distance}}km</text>
					</view>
				</view>
			</view>
			<view class="mainline">
				<view class="mainlinetext">购买数量<text>*</text></view>
				<u-number-box v-model="number" :min="1" :max="1000000" :input-width="182" :size="32" :input-height="60"
					@change="valChange" class="steps_"></u-number-box>
			</view>
			<!-- <view class="mainline" @click="setdate=true">
				<view class="mainlinetext">看货时间<text>*</text></view>
				<view class="mainlineposition" v-if="stoptime.length>0">{{stoptime[0]}}年 {{stoptime[1]}}月
					{{stoptime[2]}}日
				</view>
				<text class="mainlineposition" v-else>请选择看货时间</text>
				<u-icon name="arrow-right" color="#999999" size="24" class="mainicon"></u-icon>
			</view> -->
			<picker mode="date" :value="startDate" :start="nowtime" @change="bindDateChange">
				<view class="mainline">
					<view class="mainlinetext">看货时间<text>*</text></view>
					<text class="mainlineposition" v-if="!setdate">请选择看货时间</text>
					<view class="mainlineposition" v-else>{{startDate}}</view>
					<u-icon name="arrow-right" color="#999999" size="24" class="mainicon"></u-icon>
				</view>
			</picker>

			<simple-address ref="simpleAddress" :pickerValueDefault="cityPickerValueDefault" @onConfirm="onConfirm"
				themeColor="#007AFF"></simple-address>
			<view class="mainline" @tap="openAddres">
				<view class="mainlinetext">出发地区<text>*</text></view>
				<view class="mainlineposition" v-if="!address[0]">请选择出发地区</view>
				<view class="mainlineposition clamp1" v-if="address[0]">{{address[0]}} {{address[1]}} {{address[2]}}
				</view>
				<u-icon name="arrow-right" color="#999999" size="24" class="mainicon"></u-icon>
			</view>

			<view class="mainline">
				<view class="mainlinetext" style="padding: 10rpx 0;">备注说明</view>
				<u-input v-model="note" type="textarea" :border="false" placeholder="请备注看货要求(50字以内)" :autoHeight="true"
					class="mainlineinput" :clearable="false" maxlength="50" />
			</view>
		</view>
		<view class="bottomsbox">
			<view class="submitbtn" @click="submit">提交看货</view>
		</view>
		<!-- <u-picker mode="time" v-model="setdate" @confirm="getstoptime" start-year="2021" end-year="2121"></u-picker> -->
	</view>
</template>

<script>
	import amapFile from "../../static/js/amap-wx.js"
	import App from '@/App.vue';
	var _this, shak;
	export default {
		data() {
			return {
				settingdata: {
					name: '预约看货',
					bgColor: '#fff',
					titleColor: '#000000',
					showNav: true,
					showTypes: 0,
					position: "orderlooks"
				},
				goods: {
					merchant_info: {}
				},
				number: 1,
				// 收货地址
				address: ["", "", ""],
				// 选择时间
				setdate: false,
				// 选择时间
				stoptime: [],
				note: "",
				suborderimags: "",
				cityPickerValueDefault: [0, 0, 1],
				nowtime: '', //当前时间
				startDate: '',
			}
		},
		onLoad(e) {
			_this = this
			_this.getimg()
			_this.getcity()
			_this.sku_unique = e.sku_unique ? e.sku_unique : ''
			_this.id = e.id
		},
		onShow() {
			_this.getDate()
			if (wx.getStorageSync('address')) {
				_this.getdata()
			} else {
				wx.getLocation({
					type: 'wgs84',
					success: (location) => {
						_this.getcity()
					},
					fail: (res) => {
						_this.openLocation()
					},
				})
			}
		},
		methods: {
			bindDateChange: function(e) {
				_this.startDate = e.target.value
				_this.setdate = true
				console.log(_this.startDate)
			},
			getimg() {
				_this.$api.get("suborderimags", {}).then(data => {
					_this.suborderimags = data
				});
			},
			// 获取数据
			getdata() {
				_this.$api.get("goodsinfo", {
					id: _this.id,
					lon: uni.getStorageSync("address").long,
					lat: uni.getStorageSync("address").lat,
					sku_unique: this.sku_unique ? this.sku_unique : ''
				}).then(data => {
					_this.goods = data
				});
			},
			// 强制用户开启地址
			openLocation() {
				// 请求新的地址地址
				wx.getSetting({
					success: (res) => {
						if (!res.authSetting['scope.userLocation']) {
							wx.showModal({
								content: '检测到您没打开定位权限，是否去设置打开？',
								showCancel: false,
								success: function(res) {
									if (res.confirm) {
										wx.openSetting()
									}
								},
							});
						} else {
							_this.getcity()
						}
					}
				})
			},
			// 获取当前位置
			getcity() {
				new amapFile.AMapWX({
					key: App.mapkey
				}).getRegeo({
					success: function(data) {
						let address = data[0].regeocodeData.addressComponent
						wx.setStorageSync("address", {
							"city": address.city,
							"long": address.streetNumber.location.split(',')[0],
							"lat": address.streetNumber.location.split(',')[1]
						})
						_this.address = [address.province, address.city, address.district]
						console.log(_this.address)
						_this.getdata()
					}
				})
			},
			//跳转到指南
			torichtext: function() {
				uni.navigateTo({
					url: "/home/index/sourthroad"
				})

			},
			valChange: function(e) {},
			submit: function() {
				console.log(_this.setdate)
				_this.click_shake(function() {
					if (!_this.setdate) {
						wx.showToast({
							title: '请选择看货时间',
							icon: "none"
						})
						return
					}
					let data = [{
						value: _this.address[0],
						type: 1,
						title: "请选择出发地区"
					}]
					if (_this.$plug.verify(data)) {
						_this.$api.post("subscribe", {
							goods_id: _this.id,
							number: _this.number,
							sku_unique: _this.sku_unique ? _this.sku_unique : '',
							lookgoodstime: _this.startDate,
							city: _this.address.join(','),
							client_note: _this.note
						}).then(data => {

							// 预约看货成功后,将消息存入本地 
							data.type = 1
							uni.setStorageSync('seeorder', data)
							//发送websoket
							wx.connectSocket({
								url: _this.$api.connectUrl(),
								header: {
									'content-type': 'application/json'
								},
								method: "get",
								success: (data) => {
									console.log(data)
									//如果链接成功，则监听 WebSocket 连接打开事件
									if (data.errMsg == "connectSocket:ok") {
										//监听 WebSocket 连接打开事件
										wx.onSocketOpen(() => {
											let info = {
												type: "chatMessage",
												mine: {
													content: uni
														.getStorageSync(
															'seeorder'),
													formid: uni.getStorageSync(
														'seeorder').formid,
													type: 1
												},
												to: {
													toid: uni.getStorageSync(
														'seeorder').toid,
												}
											}
											console.log(info, 'WebSocket 连接打开')
											wx.sendSocketMessage({
												data: JSON.stringify(info),
												success: function() {
													console.log(
														'发送求购商品成功')
													wx.removeStorageSync(
														'seeorder')
													// 关闭链接
													wx.closeSocket();
												}
											})
										})
									}
								}
							})

							uni.showModal({
								title: '提交成功',
								content: '你的看货信息已经发送给卖家，请耐心等待确认，也可前往聊一聊或打电话直接联系卖家。',
								confirmText: "我知道了",
								confirmColor: "#38BE8C",
								showCancel: false,
								success: function(res) {
									if (res.confirm) {
										uni.redirectTo({
											url: '/personal/supplyList?type=1'
										})
									}
								}
							});
						});
					}
				})
			},
			// 切换城市
			openAddres() {
				_this.$refs.simpleAddress.open();
			},
			onConfirm(e) {
				_this.address = e.label.split('-');
			},

			//设置求购结束时间
			// getstoptime(e) {
			// 	_this.stoptime = [e.year, e.month, e.day]
			// 	_this.setdate = false
			// },
			// 防抖点击
			click_shake(fn) {
				if (!_this.shake) {
					fn && typeof fn === 'function' && fn()
					_this.shake = true
					setTimeout(function() {
						_this.shake = false
					}, 500)
				}
			},
			getDate() {
				const date = new Date();
				let year = date.getFullYear();
				let month = date.getMonth() + 1;
				let day = date.getDate();
				month = month > 9 ? month : '0' + month;
				day = day > 9 ? day : '0' + day;
				this.nowtime = `${year}-${month}-${day}`
				console.log(`${year}-${month}-${day}`, this.nowtime)
				return `${year}-${month}-${day}`;
			}
		}
	}
</script>

<style lang="scss" scoped>
	.manageimgbox {
		width: 100%;
		background: #F2F2F2;

		.managebox {
			margin: 19rpx 25rpx 24rpx 25rpx;
			border-radius: 10rpx;
			position: relative;

			image {
				width: 100%;
			}

			text {
				position: absolute;
				top: 23rpx;
				right: 40rpx;
				color: #666666;
				font-size: 26rpx;
			}
		}
	}

	.contentsbox {
		margin: 0 25rpx;
		padding-bottom: 110rpx;
		padding-bottom: calc(env(safe-area-inset-bottom) + 110rpx);
		display: flex;
		flex-direction: column;
		background-color: #fff;

		.contents_shops {
			padding: 40rpx 0 30rpx 0;
			display: flex;
			flex-direction: row;
			align-items: center;
			justify-content: space-between;
			border-bottom: 2rpx solid #E6E6E6;

			.storesinfo {
				display: flex;
				flex-direction: row;
				align-items: flex-end;

				.name {
					color: #333333;
					font-size: 32rpx;
					font-weight: bold;
				}

				image {
					width: auto;
					height: 36rpx;
					margin-left: 22rpx;
				}

				.xinyong_box {
					display: flex;
					flex-direction: row;
					align-items: center;
					margin-left: 8rpx;

					.xin {
						width: 30rpx;
						height: 30rpx;
						background: #00C575;
						border-radius: 3px 0 0 3rpx;
						text-align: center;
						line-height: 30rpx;
						color: #fff;
						font-size: 20rpx;
					}

					.xin_number {
						background-color: #fff;
						border: 2rpx solid #00C575;
						border-radius: 0 3rpx 3rpx 0;
						color: #00C575;
						font-size: 20rpx;
						text-align: center;
						height: 30rpx;
						line-height: 30rpx;
						min-width: 46rpx;
					}
				}
			}
		}

		.shops_details {
			display: flex;
			flex-direction: row;
			padding: 27rpx 0;

			image {
				width: 180rpx;
				height: 180rpx;
				border-radius: 5rpx;
			}

			.details_info {
				width: 460rpx;
				display: flex;
				flex-direction: column;
				margin-left: 24rpx;

				.detailname {
					color: #222222;
					font-size: 30rpx;
				}

				.hot_info {
					margin-top: 25rpx;
					display: flex;
					flex-direction: row;
					align-items: center;

					.hot_price {
						font-size: 30rpx;
						color: #F73F3F;
						font-weight: bold;
						display: flex;
						flex-direction: row;
						align-items: flex-end;

						text {
							font-weight: normal;
							font-size: 24rpx;
						}
					}

					.hot_looks {
						font-size: 22rpx;
						color: #999999;
						margin-left: 17rpx;
					}
				}

				.shopsadd {
					display: flex;
					flex-direction: row;
					align-items: center;
					margin-top: 38rpx;

					text {
						color: #999999;
						font-size: 24rpx;
					}

					.distance {
						margin-left: 13rpx;
					}
				}
			}
		}

		.mainline {
			display: flex;
			flex-direction: row;
			margin-top: 52rpx;

			.mainlinetext {
				color: #333333;
				font-size: 30rpx;
				font-weight: bold;

				text {
					color: #FF0000;
				}
			}

			.steps_ {
				margin-left: 74rpx;
			}

			.mainlineinput {
				margin-left: 74rpx;
				flex: 1;
				font: 30rpx;
				color: #333333;
				flex: 1;
			}

			.sysmol {
				margin-left: 20rpx;
				color: #333333;
				font-size: 30rpx;
			}

			.mainlineposition {
				margin-left: 76rpx;
				color: #333333;
				font-size: 30rpx;
				flex: 1;
			}

			.mainicon {
				margin-left: 20rpx;
			}
		}
	}

	.bottomsbox {
		height: 110rpx;
		display: flex;
		justify-content: center;
		align-items: center;
		background-color: #fff;
		position: fixed;
		bottom: 0;
		width: 100%;
		padding-bottom: calc(env(safe-area-inset-bottom));
		z-index: 99;

		.submitbtn {
			width: 680rpx;
			height: 90rpx;
			background: linear-gradient(86deg, #38BE8C, #05ADBB);
			border-radius: 45rpx;
			text-align: center;
			color: #fff;
			font-size: 34rpx;
			font-weight: bold;
			line-height: 90rpx;
		}
	}
</style>
